package com.wxc.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author wxc
 * @date 2021年10月01日 16:50
 */
public class SocketUtil {
    public static final int SERVER_PORT = 9998;
    private static final Logger logger = LoggerFactory.getLogger(SocketUtil.class);

    /**
     * @date 2021-10-02 22:29
     * start Server
     */
    public static ServerSocket startServer() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(SERVER_PORT);
            logger.info("Server start on port:{}", serverSocket.getLocalPort());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return serverSocket;
    }

    public static Socket getSocket() {
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", SERVER_PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return socket;
    }

    public static void close(Socket socket) {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
